<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

defined('_VALID_MOS') or die('Direct Access to this location is not allowed.');

$current_match	= LM::getMatch();
$match = null;

if($current_match > 0){
	$match = LM::getLMTable("Match");
	$match->load($current_match);
}

if(!$match instanceof LM_Table_Match){
	LM::redirect("matches");
	exit();
}

$home = $match->getTeam1();
$away = $match->getTeam2();

$teamid	= intval(LM::getSessionValueFromRequest("_filterbylineupteam_", "lineupteam", $home->getID()));
		
switch($teamid){
	case $home->getID():
		$lineupteam = $home;
	break;
			
	case $away->getID():
		$lineupteam = $away;
	break;
}

switch($task){
	
	case 'save':
		
		$errors	= 0;
	
		$players = $match->getLineup($lineupteam);
		
		if(isset($_POST["cid"]) && is_array($_POST["cid"])){
			
			$query = "INSERT INTO #__leagueman_lineup VALUES(%s, %s, %s)";
			
			foreach($_POST["cid"] AS $player){
				if(!in_array($player, $players)){
					$database->setQuery(sprintf($query, $match->match_id, $lineupteam->getID(), intval($player)));
					if($database->query()){
						$playermap_query = "UPDATE #__leagueman_playermap SET cnt_matches = cnt_matches + 1
											 WHERE playermap_project = %s AND playermap_team = %s AND playermap_player = %s";
						$database->setQuery(sprintf($playermap_query, $project->getID(), $lineupteam->getID(), intval($player)));
						$database->query();
					} else {
						$errors++;
					}
				}
			}
		}
		
		$query = "DELETE FROM #__leagueman_lineup 
						WHERE lineup_match = %s AND lineup_team = %s AND lineup_player = %s";
		
		foreach($players AS $player){
			if(!in_array($player, $_POST["cid"])){
				$database->setQuery(sprintf($query, $match->match_id, $lineupteam->getID(), $player));
				if($database->query()){
					$playermap_query = "UPDATE #__leagueman_playermap SET cnt_matches = cnt_matches - 1
											 WHERE playermap_project = %s AND playermap_team = %s AND playermap_player = %s";
						$database->setQuery(sprintf($playermap_query, $project->getID(), $lineupteam->getID(), intval($player)));
						$database->query();
				} else {
					$errors++;
				}
			}
		}
		
		LM::redirect("matches", "Lineup edit with $errors Errors");
	break;
	
	case 'cancel':
		LM::redirect("matches");
		exit();
	break;
	
	case 'reset':
		$database->setQuery("DELETE FROM #__leagueman_lineup WHERE lineup = " . $match->match_id);
		LM::redirect("matches");
		exit();
	break;
	
	default:
		
		$playermap	= LM::getLMTable("Playermap");
		$playermap->filterbyProject($project->getID());
		$playermap->filterbyTeam($teamid);
		$players	= $playermap->getRows("", 0,0);
		
		$rows = $match->getLineup($lineupteam);
		
		$teams[] = mosHTML::makeOption($home->getID(), $home->getName());
		$teams[] = mosHTML::makeOption($away->getID(), $away->getName());
		$teamlist = mosHTML::selectList($teams, "lineupteam", 'class="inputbox" size="1" onChange="javascript:document.adminForm.submit();"', "value", "text", $teamid);
		
		$template = LM::getTemplate();
		
		$template->assign("view",		"admin/edit_lineup.tpl.php");
		$template->assign("project",	$project);
		$template->assign("team",		$team);
		$template->assign("match",		$match);
		
		$template->assign("teamlist",	$teamlist);
		$template->assign("players", 	$players);
		$template->assign("rows",		$rows);
		
		$template->assign("my",			$my);
		$template->display("admin/view.tpl.php");
	break;
}

?>